//
//  UIScrollViewExtension.swift
//  YYProject
//
//  Created by 陆文宇 on 2017/8/31.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

extension UIScrollView {
    
    func yy_showNoMoreData(with message: String) {
        if let view = viewWithTag(1212) {
            view.removeFromSuperview()
        }
        
        let imageView = UIImageView(frame: CGRect(x: (screenWidth - 40) / 2 - 66.5, y: 0, width: 133, height: 80))
        imageView.image = #imageLiteral(resourceName: "nodata")
        let messageLabel = UILabel(frame: CGRect(x: 0, y: 90, width: screenWidth - 40, height: 100), aFont: mainFont, aString: message)
        messageLabel.textAlignment = .center
        messageLabel.textColor = .darkGray
        let label = UILabel(frame: CGRect(x: 0, y: 90 + messageLabel.yy_height, width: screenWidth - 40, height: 20))
        label.textAlignment = .center
        label.text = "先去别的地方逛逛吧～"
        label.textColor = .darkGray
        label.font = mainFont
        
        let nodataView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth - 40, height: 90 + messageLabel.yy_height + 20))
        nodataView.center = CGPoint(x: bounds.width / 2, y: nodataView.yy_height / 2 + 30)
        
        nodataView.addSubview(imageView)
        nodataView.addSubview(messageLabel)
        nodataView.addSubview(label)
        
        var rect = screenBounds
        if let tableView = self as? UITableView {
            if let tableHeaderView = tableView.tableHeaderView {
                rect.origin.y = tableHeaderView.yy_height
            }
        } else if let collectionView = self as? UICollectionView {
            rect.origin.y = (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).headerReferenceSize.height
        }
        
        let backView = UIView(frame: rect)
        backView.tag = 1212
        backView.isUserInteractionEnabled = false
        backView.backgroundColor = backgroundColor
        backView.addSubview(nodataView)
        addSubview(backView)
    }
    
    func yy_dismissNoMoreData() {
        if let view = viewWithTag(1212) {
            view.removeFromSuperview()
        }
    }
    
}
